package affd.logic;

import affd.hfst.*;
import java.io.FileInputStream;
import java.io.DataInputStream;
import java.util.ArrayList;


public class HfstStarter {
	
	private String transducerFileName = "mor-omorfi.hwfst.ol";
	private TransducerWeighted transducer;

	/**
	 * Constructor for the HfstStarter
	 * @throws java.io.IOException
	 */
    public HfstStarter() throws java.io.IOException {
    	FileInputStream transducerfile = null;
    	try { 
    		transducerfile = new FileInputStream(transducerFileName); 
    	}
    	catch (java.io.FileNotFoundException e) {
    		System.err.println("File not found: couldn't read transducer file " + transducerFileName + ".");
    		System.exit(1);
    	}
    	
    	System.out.println("Reading header...");
    	TransducerHeader h = new TransducerHeader(transducerfile);
    	DataInputStream charstream = new DataInputStream(transducerfile);
    	System.out.println("Reading alphabet...");
    	TransducerAlphabet a = new TransducerAlphabet(charstream, h.getSymbolCount());
    	System.out.println("Reading transition and index tables...");
    	
    	if (h.isWeighted()) {
    		System.out.println("Weighted");
    		transducer = new TransducerWeighted(transducerfile, h, a);  		
    	} 
    	else {
    		//System.out.println("Normal");
    		//transducer = new Transducer(transducerfile, h, a);
    	}   	   	
	}
    
    /**
     * Checks if the given word is recognized by HFST to be a pronoun
     * 
     * @param word The word which is checked for being a pronoun
     * @return Boolean value which indicates if the word was recognized to be a pronoun
     */
    public boolean wasPronoun(String word) {	
    	return HfstOptimizedLookup.wasPronoun(transducer, word);
    }
    
    
    /**
     * 
     * @param basicForm is the basic form of the word.
     * @param word is the basicForm in some of its cases.
     * @return String in the form case;number, where case is one of 16 finnish language cases
     * NOM, PAR, GEN, ... and number is either plural - PL or singal - SG.
     */
    
    public String getCaseAndNumber(String basicForm, String word) {
    	return HfstOptimizedLookup.getCaseAndNumber(transducer, basicForm, word);
    }
    
    //public boolean wasPureName(String word) {	
    //	return HfstOptimizedLookup.wasPureName(transducer, word);
    //}
    
    /** 
     * Takes a word and returns an ArrayList of its basic forms.
     * 
     * @param word in some form.
	 * @return The word in its basic forms.
     * 
     * */
    public ArrayList<String> getBasicForms(String word) {
    	return HfstOptimizedLookup.runTransducer(transducer, word);	
    }
}



	
